/*************************************************************************************
 **																					**
 **	radioapi.h			SX1272 RFIC													**
 ** 																				**
 **************************************************************************************
 **																					**
 ** Written By:	Steve Montgomery													**
 **				Digital Six Laboratories LLC										**
 ** (c)2012,2013,2014 Digital Six Labs, All rights reserved							**
 **																					**
 **************************************************************************************/
//
// Revision History
//
// Revision		Date	Reviser		Description
// ===================================================================================
// ===================================================================================
/*! \addtogroup Radio Radio API
 *
 * \ingroup PortedAPIs
 * @{
 * \details This is the radio API for the SX1272H radio.
 *
 */
#ifndef RADIOAPI_DEFS_H
#define RADIOAPI_DEFS_H

#include "..\..\MicrocontrollerAPI\RL78\microapi.h"
#include "sx1272_defs.h"

#define MAXTIMERS 16

/*! \details Enumerates blocking modes
 *
 */
typedef enum {
	/*! Blocking call.  Will complete before returning*/
	kBlockingCall,
	/*! Event driven call.  Will return immediately */
	kEventDriven
} tBlockingModes;

/*! \details Defines all of the different states that the radio can be in.
 */
typedef enum {
	/*! Transmitting */
	kTxMode = 0,
	/*! Listening for incoming packet */
	kIdleRxMode,
	/*! Receiving incoming packet */
	kRxMode,
	/*! Receiving an acknowledgment */
	kRxAckMode,
	/*! Hopping in progress */
	kHopMode
} tRadioStates;
/*! \details Enumerates receiver modes
 *
 */
typedef enum {
	/*! Single Packet with CAD - Blocking Call*/
	kBlockingCADSingle,
	/*! Single packet without CAD  - Blocking Call*/
	kBlockingNoCADSingle,
	/*! Single Packet with CAD - Event driven*/
	kEventingCADSingle,
	/*! Single packet without CAD  - Event driven*/
	kEventingNoCADSingle,
	/*! Continuous packets with CAD - Event driven */
	kEventingCADContinuous,
	/*! Continuous packets without CAD - Event driven */
	kEventingNoCADContinuous,
	/*! Continuous packets with CAD  - Blocking Call*/
	kBlockingCADContinuous,
	/*! Continuous packets without CAD  - Blocking Call*/
	kBlockingNoCADContinuous,
} tReceiveModes;
/*! \details Defines all of the different packet types that are available
 */
typedef enum tPacketTypes{
	/*! Unicast packet (point to point) with acknowledgment */
	kUniAckPacketType = 0,
	/*! Unicast packet(point to point) without acknowledgment */
	kUniNoAckPacketType,
	/*! Multicast packet.  This is a broadcast packet to everyone on the network */
	kMulticastPacketType,
	/*! Acknowledgment packet.  This is sent in response to a UNIACK packet	 */
	kAckPacketType,
	/*! Sync packet.  This is used to periodically sync a master and 1+ slaves */
	kSyncPacketType,
	kNone

} tPacketTypes;

enum {
	kInterruptP0,
	kInterruptP1,
	kInterruptP2,
	kInterruptP3,
	kInterruptP4,
	kInterruptP5,
	kInterruptK0 = 0x80,
	kInterruptK1,
	kInterruptK2,
	kInterruptK3
};

enum {
	flagRadioModeReady = 0x80,
	flagReceiverReady = 0x40,
	flagTransmitterReady = 0x20,
	flagPllLock = 0x10,
	flagRssiThreshold = 0x08,
	flagReceiverTimeout = 0x04,
	flagIntermediateMode = 0x02,
	flagSyncMatch = 0x01,
	flagFifoFull = 0x80,
	flagFifoNotEmpty = 0x40,
	flagFifoLevel = 0x20,
	flagFifoOverrun = 0x10,
	flagPacketSent = 0x08,
	flagPayloadReady = 0x04,
	flagCRCGood = 0x02
};

typedef enum {
	kSleepMode = 0x00,
	kStandbyMode = 0x01,
	kFSTXMode = 0x02,
	kTransmitMode = 0x03,
	kFSRXMode = 0x04,
	kRxContinuousMode = 0x05,
	kRxSingleMode = 0x06,
	kCADMode = 0x07,
	kLoraMode = 0x80,
	kSharedRegMode = 0x40
} tOperatingModes;
// ***********************************************************************************
/*! \details Send a packet using the radio's built in packet engine.
 */
U8 RadioSendPacket(U8 length, /*! Length of packet SDU (service data unit or payload) */
		U8 *SDU, /*! Pointer to buffer containing packet SDU (service data unit or payload) */
		tBlockingModes blocking /*! 1=make this a blocking call, 0=use interrupts instead*/);
/*! \details Set the maximum time to wait for a packet before bailing.
 *
 */
U8 RadioSetReceiveTimeout(U16 to /*! timeout in mSec */);
/*! \details Put radio in receive mode and start listening for an incoming packet
 */
U8 RadioReceivePacket(tReceiveModes rxMode /*! CAD/NoCAD, Single/Continuous */);
/*! \details Initialize the RadioAPI.  On return, the radio hardware will be configured and will be asleep.
*/
U8 RadioInitializeAPI();
/*! \details Sets the preamble count for subsequent packet transmissions
 *
 */
void RadioSetPreambleCount(UU16 preambleCount /*!Count in bytes */);
/*! \details Set the radio to standby mode.  This is the lowest power mode of the radio.
 *
 */
void RadioStandbyMode();
/*! Put radio in sleep mode
 *
 */
void RadioSleepMode();

/*! \details Sets the radio channel.
 */
void RadioSetChannel(U8 channel /*! Desired channel.  Valid channels are 0-24*/);
/*! \details Sets the radio transmit power.  If the RFIC is a 1231H, the PA Boost will automatically be used for the high power setting.
 */
void RadioSetTxPower(
		U8 power /*! Desired power level.  Bits 6,7,8 turn on PA0,PA1,PA2 respectively.  Bits 0-4 set power level in 1dB increments.  See 3.4.6 in SX1231 datasheet */);

/*! \details Reads RSSI from the SX1272.
 *  \return RSSI value (RSSI = -125+value)
 */
U8 RadioGetRSSI();
/*! \details Read SNR value from SX1272.
 *  \return SNR value
 */
U8 RadioGetSNR();


/*! \details Places the radio in ready idle mode.  Everything is on except for RX and TX circuitry.
 U8 IdleMode();

/*! \details Set the local MAC address
 *
 */
void RadioSetMACAddress(UU32 macAddress/*! 32 bit MAC address */);
/*! \details Gets temperature in 2's compliment
 */
S8 RadioGetTemperature();
/*! \details Sets the encryption key
 */
void RadioSetEncryptionKey(UU256 key/*! ES-256 Key*/);
/*! \details Gets the current radio state
 */
U8 RadioGetRadioState();

/*! \details Get flags from SX1272
 *  \returns flags
 */
U8 RadioGetRFICFlags();

/*! \details Is radio in an idle state, ready for RX or TX
 *  \returns 1 if the radio is idle, 0 if not.
 */
U8 RadioIsIdle();




extern void NotifyRadioPacketReceived(U8 length, U8 *txBuffer, S8 rssi);
extern void NotifyRadioPacketSent();
extern void NotifyRadioPacketSendError();
extern void NotifyRadioReceiveError();
extern void NotifyRadio1Second();
extern void NotifyRadio1MilliSecond();
// These are the callbacks from the microapi for various interrupt services
extern void HandleInterrupt(U8 intType);
extern void Handle1MsInterrupt();
extern void Handle1SecInterrupt();
// This is a published callback to the MAC layer that notifies the MAC layer when a packet has
// been received. This is used if ReceivePacket(0) is called.
extern void HandleRxPacket();

#define pinCRCOK pinRadioDIO0
#define pinPKTSENT pinRadioDIO0
#define pinLOCKPLL pinRadioDIO1
#define pinTIMEOUT pinRadioDIO1
#define pinFIFONE pinRadioDIO2
#define pinSYNCADDR pinRadioDIO3
#define pinTXRDY pinRadioDIO4
#define pinRSSI pinRadioDIO4
#define pinMODERDY pinRadioDIO5

#define kModeChangeTimeout 8192

#endif
/*!
 * @}
 */
